using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Input;
using Common;
using Common.Command;
using Common.Enum;
using Common.NotifyEvent;
using SSettings.ExperimentColumn;
using SSettings.SoftSetting;

namespace SSettings
{
	[Serializable]
	public class ConfigSettings : NotifyPropertyChanged
	{
		private ICommand _RestALLSetting;

		private ObservableCollection<QuickExperimentData> _QuickExperimentDatas;

		private static ConfigSettings _Instance;

		private ChannelSetting _ChannelSetting = new ChannelSetting();

		private bool _ProjectMainPageIsVisible = true;

		private MemorySettings _MemorySettings = new MemorySettings();

		private ExperimentSetting _ExperimentSetting = new ExperimentSetting();

		private BasicSetting _BasicSetting = new BasicSetting();

		private CurveSetting _CurveSetting = new CurveSetting();

		private AQColumns _AQColumns = (AQColumns)ColumnsFactory.CreateColumns(EProjectType.AQ);

		private MeltColumns _MeltColumns = (MeltColumns)ColumnsFactory.CreateColumns(EProjectType.Melt);

		private HRMColumns _HRMColumns = (HRMColumns)ColumnsFactory.CreateColumns(EProjectType.HRM);

		private ADColumns _ADColumns = (ADColumns)ColumnsFactory.CreateColumns(EProjectType.AD);

		private RQColumns _RQColumns = (RQColumns)ColumnsFactory.CreateColumns(EProjectType.RQ);

		private TQColumns _TQColumns = (TQColumns)ColumnsFactory.CreateColumns(EProjectType.TQ);

		private IAColumns _IAColumns = (IAColumns)ColumnsFactory.CreateColumns(EProjectType.IA);

		private AQColumns _FastCalColumns = (AQColumns)ColumnsFactory.CreateColumns(EProjectType.FastCal);

		private History _History = new History();

		private ProjectReportSetting _ProjectReportSetting = new ProjectReportSetting();

		private PrintSetting _PrintSetting = new PrintSetting();

		public ICommand RestALLSetting
		{
			get
			{
				if (_RestALLSetting == null)
				{
					_RestALLSetting = new SimpleDelegateCommand(RestALLSettingExecute);
				}
				return _RestALLSetting;
			}
		}

		public ObservableCollection<QuickExperimentData> QuickExperimentDatas
		{
			get
			{
				if (_QuickExperimentDatas == null)
				{
					_QuickExperimentDatas = new ObservableCollection<QuickExperimentData>();
				}
				return _QuickExperimentDatas;
			}
			set
			{
				_QuickExperimentDatas = value;
			}
		}

		public ChannelSetting ChannelSetting
		{
			get
			{
				return _ChannelSetting;
			}
		}

		public bool ProjectMainPageIsVisible
		{
			get
			{
				return _ProjectMainPageIsVisible;
			}
			set
			{
				_ProjectMainPageIsVisible = value;
			}
		}

		public MemorySettings MemorySettings
		{
			get
			{
				return _MemorySettings;
			}
		}

		public ExperimentSetting ExperimentSetting
		{
			get
			{
				return _ExperimentSetting;
			}
			set
			{
				_ExperimentSetting = value;
			}
		}

		public BasicSetting BasicSetting
		{
			get
			{
				return _BasicSetting;
			}
		}

		public CurveSetting CurveSetting
		{
			get
			{
				return _CurveSetting;
			}
		}

		public AQColumns AQColumns
		{
			get
			{
				return _AQColumns;
			}
			set
			{
				_AQColumns = value;
				OnPropertyChanged("AQColumns");
			}
		}

		public MeltColumns MeltColumns
		{
			get
			{
				return _MeltColumns;
			}
			set
			{
				_MeltColumns = value;
				OnPropertyChanged("MeltColumns");
			}
		}

		public HRMColumns HRMColumns
		{
			get
			{
				return _HRMColumns;
			}
			set
			{
				_HRMColumns = value;
				OnPropertyChanged("HRMColumns");
			}
		}

		public ADColumns ADColumns
		{
			get
			{
				return _ADColumns;
			}
			set
			{
				_ADColumns = value;
				OnPropertyChanged("ADColumns");
			}
		}

		public RQColumns RQColumns
		{
			get
			{
				return _RQColumns;
			}
			set
			{
				_RQColumns = value;
				OnPropertyChanged("RQColumns");
			}
		}

		public TQColumns TQColumns
		{
			get
			{
				return _TQColumns;
			}
			set
			{
				_TQColumns = value;
				OnPropertyChanged("TQColumns");
			}
		}

		public IAColumns IAColumns
		{
			get
			{
				return _IAColumns;
			}
			set
			{
				_IAColumns = value;
				OnPropertyChanged("IAColumns");
			}
		}

		public AQColumns FastCalColumns
		{
			get
			{
				return _FastCalColumns;
			}
			set
			{
				_FastCalColumns = value;
				OnPropertyChanged("FastCalColumns");
			}
		}

		public History History
		{
			get
			{
				return _History;
			}
		}

		public ProjectReportSetting ProjectReportSetting
		{
			get
			{
				return _ProjectReportSetting;
			}
			set
			{
				_ProjectReportSetting = value;
			}
		}

		public PrintSetting PrintSetting
		{
			get
			{
				return _PrintSetting;
			}
			set
			{
				_PrintSetting = value;
			}
		}

		public void RestALLSettingExecute()
		{
			ExperimentSetting.ReSet();
		}

		public static ConfigSettings GetInstance()
		{
			if (_Instance == null)
			{
				_Instance = new ConfigSettings();
			}
			return _Instance;
		}

		public ConfigSettings Clone()
		{
			ConfigSettings configSettings = new ConfigSettings();
			StreamEx streamEx = new StreamEx();
			SaveToStream(streamEx);
			streamEx.Seek(0L, SeekOrigin.Begin);
			configSettings.ReadFromStream(streamEx);
			return configSettings;
		}

		public ConfigSettings()
		{
			AQColumns.SetVisibilityDefault();
			FastCalColumns.SetVisibilityDefault();
			MeltColumns.SetVisibilityDefault();
			HRMColumns.SetVisibilityDefault();
			ADColumns.SetVisibilityDefault();
			RQColumns.SetVisibilityDefault();
			TQColumns.SetVisibilityDefault();
			IAColumns.SetVisibilityDefault();
		}

		public GridColumns GetGirdColumnsClone(EProjectType type)
		{
			switch (type)
			{
			case EProjectType.Melt:
				return GetInstance().MeltColumns.Clone();
			case EProjectType.HRM:
				return GetInstance().HRMColumns.Clone();
			case EProjectType.AD:
				return GetInstance().ADColumns.Clone();
			case EProjectType.RQ:
				return GetInstance().RQColumns.Clone();
			case EProjectType.TQ:
				return GetInstance().TQColumns.Clone();
			case EProjectType.FastCal:
				return GetInstance().FastCalColumns.Clone();
			default:
				return GetInstance().AQColumns.Clone();
			}
		}

		public GridColumns GetGirdColumnsCloneByColumn(GridColumns gridColumns)
		{
			if (gridColumns is FastCalColumns)
			{
				return GetInstance().FastCalColumns.Clone();
			}
			if (gridColumns is MeltColumns)
			{
				return GetInstance().MeltColumns.Clone();
			}
			if (gridColumns is HRMColumns)
			{
				return GetInstance().HRMColumns.Clone();
			}
			if (gridColumns is ADColumns)
			{
				return GetInstance().ADColumns.Clone();
			}
			if (gridColumns is RQColumns)
			{
				return GetInstance().RQColumns.Clone();
			}
			if (gridColumns is TQColumns)
			{
				return GetInstance().TQColumns.Clone();
			}
			if (gridColumns is IAColumns)
			{
				return GetInstance().IAColumns.Clone();
			}
			return GetInstance().AQColumns.Clone();
		}

		public GridColumns GetGirdColumnsCommon(EProjectType type)
		{
			switch (type)
			{
			case EProjectType.Melt:
				return GetInstance().MeltColumns;
			case EProjectType.HRM:
				return GetInstance().HRMColumns;
			case EProjectType.AD:
				return GetInstance().ADColumns;
			case EProjectType.RQ:
				return GetInstance().RQColumns;
			case EProjectType.TQ:
				return GetInstance().TQColumns;
			case EProjectType.FastCal:
				return GetInstance().FastCalColumns;
			default:
				return GetInstance().AQColumns;
			}
		}

		public GridColumns GetGirdColumnsCommonByColumn(GridColumns gridColumns)
		{
			if (gridColumns is FastCalColumns)
			{
				return GetInstance().FastCalColumns;
			}
			if (gridColumns is MeltColumns)
			{
				return GetInstance().MeltColumns;
			}
			if (gridColumns is HRMColumns)
			{
				return GetInstance().HRMColumns;
			}
			if (gridColumns is ADColumns)
			{
				return GetInstance().ADColumns;
			}
			if (gridColumns is RQColumns)
			{
				return GetInstance().RQColumns;
			}
			if (gridColumns is TQColumns)
			{
				return GetInstance().TQColumns;
			}
			if (gridColumns is IAColumns)
			{
				return GetInstance().IAColumns;
			}
			return GetInstance().AQColumns;
		}

		public void RestoreGirdColumnsCommonByColumn(GridColumns gridColumns)
		{
			if (gridColumns is FastCalColumns)
			{
				GetInstance().FastCalColumns = (FastCalColumns)ColumnsFactory.CreateColumns(EProjectType.FastCal);
			}
			else if (gridColumns is MeltColumns)
			{
				GetInstance().MeltColumns = (MeltColumns)ColumnsFactory.CreateColumns(EProjectType.Melt);
			}
			else if (gridColumns is HRMColumns)
			{
				GetInstance().HRMColumns = (HRMColumns)ColumnsFactory.CreateColumns(EProjectType.HRM);
			}
			else if (gridColumns is ADColumns)
			{
				GetInstance().ADColumns = (ADColumns)ColumnsFactory.CreateColumns(EProjectType.AD);
			}
			else if (gridColumns is RQColumns)
			{
				GetInstance().RQColumns = (RQColumns)ColumnsFactory.CreateColumns(EProjectType.RQ);
			}
			else if (gridColumns is TQColumns)
			{
				GetInstance().TQColumns = (TQColumns)ColumnsFactory.CreateColumns(EProjectType.TQ);
			}
			else if (gridColumns is IAColumns)
			{
				GetInstance().IAColumns = (IAColumns)ColumnsFactory.CreateColumns(EProjectType.IA);
			}
			GetInstance().AQColumns = (AQColumns)ColumnsFactory.CreateColumns(EProjectType.AQ);
		}

		public void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_BadPoints);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			list.Add(PropertyKey.Key_MarkBadPoint);
			list2.Add(BytesConverter.GetBytes(ProjectMainPageIsVisible));
			StreamEx streamEx = new StreamEx();
			ExperimentSetting.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_ExperimentSetting);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			BasicSetting.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_BasicSetting);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			CurveSetting.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_CustomImport);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			History.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_History);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			AQColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_AQColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			FastCalColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_FastCalColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			MeltColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_MeltColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			HRMColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_HRMColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			ADColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_ADColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			RQColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_RQColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			TQColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_TQColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			IAColumns.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_IAColumns);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			MemorySettings.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_MemorySettings);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			if (QuickExperimentDatas.Count > 0)
			{
				list.Add(PropertyKey.Key_QuickExperimentData);
				streamEx = new StreamEx();
				streamEx.Write(QuickExperimentDatas.Count);
				foreach (QuickExperimentData quickExperimentData in QuickExperimentDatas)
				{
					quickExperimentData.SaveToStream(streamEx);
				}
				list2.Add(streamEx.ToArray());
				streamEx.Close();
			}
			streamEx = new StreamEx();
			ProjectReportSetting.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_ProjectReportSetting);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			streamEx = new StreamEx();
			PrintSetting.SaveToStream(streamEx);
			list.Add(PropertyKey.Key_PrintSetting);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey != PropertyKey.Key_BadPoints)
			{
				stream.Seek(-4L, SeekOrigin.Current);
				return false;
			}
			propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_Index)
			{
				List<PropertyKey> keys;
				List<byte[]> dates;
				stream.ReadIndex(out keys, out dates);
				bool flag = true;
				for (int i = 0; i < keys.Count; i++)
				{
					if (!flag)
					{
						return false;
					}
					switch (keys[i])
					{
					case PropertyKey.Key_MarkBadPoint:
						ProjectMainPageIsVisible = BytesConverter.BytesToBool(dates[i]);
						break;
					case PropertyKey.Key_ExperimentSetting:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = ExperimentSetting.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_BasicSetting:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = BasicSetting.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_CustomImport:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = CurveSetting.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_History:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = History.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_AQColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = AQColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_FastCalColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = FastCalColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_MeltColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = MeltColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_HRMColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = HRMColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_ADColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = ADColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_RQColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = RQColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_IAColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = IAColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_TQColumns:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = TQColumns.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_MemorySettings:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = MemorySettings.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_QuickExperimentData:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						int num = streamEx.ReadInt();
						for (int j = 0; j < num; j++)
						{
							QuickExperimentData quickExperimentData = new QuickExperimentData();
							flag = quickExperimentData.ReadFromStream(streamEx);
							QuickExperimentDatas.Add(quickExperimentData);
						}
						foreach (QuickExperimentData quickExperimentData2 in QuickExperimentDatas)
						{
							if (quickExperimentData2.Index == -1)
							{
								quickExperimentData2.Index = QuickExperimentDatas.Max((QuickExperimentData s) => s.Index) + 1;
							}
						}
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_ProjectReportSetting:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = ProjectReportSetting.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_PrintSetting:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						flag = PrintSetting.ReadFromStream(streamEx);
						streamEx.Close();
						break;
					}
					}
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			while (true)
			{
				switch (stream.ReadKey())
				{
				case PropertyKey.Key_MarkBadPoint:
					ProjectMainPageIsVisible = stream.ReadBool();
					break;
				case PropertyKey.Key_ExperimentSetting:
					ExperimentSetting.ReadFromStream(stream);
					break;
				case PropertyKey.Key_BasicSetting:
					BasicSetting.ReadFromStream(stream);
					break;
				case PropertyKey.Key_CustomImport:
					CurveSetting.ReadFromStream(stream);
					break;
				case PropertyKey.Key_PatientColumns:
				{
					GridColumns gridColumns = new GridColumns();
					gridColumns.ReadFromStream(stream);
					break;
				}
				case PropertyKey.Key_History:
					History.ReadFromStream(stream);
					break;
				case PropertyKey.Key_AQColumns:
					AQColumns.ReadFromStream(stream);
					break;
				case PropertyKey.Key_MeltColumns:
					MeltColumns.ReadFromStream(stream);
					break;
				case PropertyKey.Key_HRMColumns:
					HRMColumns.ReadFromStream(stream);
					break;
				case PropertyKey.Key_ADColumns:
					ADColumns.ReadFromStream(stream);
					break;
				case PropertyKey.Key_RQColumns:
					RQColumns.ReadFromStream(stream);
					break;
				case PropertyKey.Key_MemorySettings:
					MemorySettings.ReadFromStream(stream);
					break;
				default:
					stream.Seek(-4L, SeekOrigin.Current);
					return true;
				}
			}
		}
	}
}
